[ -z "${ANALYSISPATH+set}" ] && echo Environment not set && exit 1
[ -z "${RESULTSPATH+set}" ] && echo Environment not set && exit 1

source "$ANALYSISPATH/createplotvars.sh"

# Sort data by data and time
SORTED=$(cat $RESULTSPATH/hdctestresults.*.csv | bash "$ANALYSISPATH/sortbydate.sh")

# Filter jumps (if necessary)
if $IGNOREJUMPS; then
  DDDATA=$(awk -F, -v "datacol=8" -f $ANALYSISPATH/ignorejumps.awk <<< "$SORTED")
  FSDATA=$(awk -F, -v "datacol=9" -f $ANALYSISPATH/ignorejumps.awk <<< "$SORTED")
  DBDATA=$(awk -F, -v "datacol=10" -f $ANALYSISPATH/ignorejumps.awk <<< "$SORTED")
else
  DDDATA="$SORTED"
  FSDATA="$SORTED"
  DBDATA="$SORTED"
fi

# Extract columns and rows
DDDATA_HDC=$(echo "$DDDATA" | grep "test_" | awk -F, -f $ANALYSISPATH/extractddtimetaken.awk)
FSDATA_HDC=$(echo "$FSDATA" | grep "test_" | awk -F, -f $ANALYSISPATH/extractfstimetaken.awk)
DBDATA_HDC=$(echo "$DBDATA" | grep "test_" | awk -F, -f $ANALYSISPATH/extractdbtimetaken.awk)
DDDATA_NOHDC=$(echo "$DDDATA" | grep "copyonly" | awk -F, -f $ANALYSISPATH/extractddtimetaken.awk)
FSDATA_NOHDC=$(echo "$FSDATA" | grep "copyonly" | awk -F, -f $ANALYSISPATH/extractfstimetaken.awk)
DBDATA_NOHDC=$(echo "$DBDATA" | grep "copyonly" | awk -F, -f $ANALYSISPATH/extractdbtimetaken.awk)

# Create results
DATAFILEHDR="# Time_ms Moving_Median Moving_Mean" 
cat <(echo $DATAFILEHDR) <(awk -F, -f "$ANALYSISPATH/movingmedianmean.awk" <<< "$DDDATA_HDC") > $DDFILE_HDC
cat <(echo $DATAFILEHDR) <(awk -F, -f "$ANALYSISPATH/movingmedianmean.awk" <<< "$FSDATA_HDC") > $FSFILE_HDC
cat <(echo $DATAFILEHDR) <(awk -F, -f "$ANALYSISPATH/movingmedianmean.awk" <<< "$DBDATA_HDC") > $DBFILE_HDC
cat <(echo $DATAFILEHDR) <(awk -F, -f "$ANALYSISPATH/movingmedianmean.awk" <<< "$DDDATA_NOHDC") > $DDFILE_NOHDC
cat <(echo $DATAFILEHDR) <(awk -F, -f "$ANALYSISPATH/movingmedianmean.awk" <<< "$FSDATA_NOHDC") > $FSFILE_NOHDC
cat <(echo $DATAFILEHDR) <(awk -F, -f "$ANALYSISPATH/movingmedianmean.awk" <<< "$DBDATA_NOHDC") > $DBFILE_NOHDC
